package com.contract.spirit.ui.webview

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.contract.commonlib.application.ApplicationProxy
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.util.CommonUtils
import com.contract.commonlib.util.StatusBarUtil
import com.contract.spirit.R
import com.contract.spirit.databinding.ActivityNotTitleWebviewBinding

/**
 * @author: witness
 * created: 2021/9/22
 * desc:
 */
class CommonWebViewActivity : BaseActivity<ActivityNotTitleWebviewBinding, WebViewModel>() {
    private val EXTRA_HAS_SHARE = "EXTRA_HAS_SHARE"
    var webViewRefresh = false
    override fun setContentView(): Int {
        return R.layout.activity_not_title_webview
    }

    override fun initData() {
        setStatusBarStyle(false)
        mBinding.root.fitsSystemWindows = false
        CommonUtils.setPaddinTop(
            mBinding.titleBar,
            0,
            StatusBarUtil.getStatusBarHeight(mContext),
            0,
            0
        )
        WebViewUtil.initWebview(this, mBinding.wv, mBinding.pbLoading)
        WebViewUtil.addJSInterface(this, mBinding.wv)
        val data = intent.data;
        if (data != null) {
            val url = data.getQueryParameter("url")
            if (!TextUtils.isEmpty(url)) {
                intent.putExtra("url", url)
            }
            val hasShare = data.getQueryParameter("hasShare")
            if (!TextUtils.isEmpty(hasShare)) {
                intent.putExtra(
                    EXTRA_HAS_SHARE,
                    hasShare.toBoolean()
                )
            }
        }
        val title = intent.getStringExtra("title")
        if (!TextUtils.isEmpty(title)) {
            mBinding.titleBar.setTitleName(title)
        }
        reset()
    }

    private fun reset() {
        loadUrl()
    }

    private fun loadUrl() {
        var url = getUrl()
        if (url != null) {
            if (!url.startsWith("http") && !url.startsWith("https")) {
                url = if (url.startsWith("/")) {
                    ApplicationProxy.instance!!.getBaseUrl() + url;
                } else {
                    ApplicationProxy.instance!!.getBaseUrl() + "/" + url;
                }
            }
            mBinding.wv.loadUrl(url)
        }
    }

    fun getUrl(): String? {
        return intent.getStringExtra("url")
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        webViewRefresh = false
        reset()
    }

    override fun onResume() {
        super.onResume()
        if (webViewRefresh) {
            mBinding.wv.url?.let { mBinding.wv.loadUrl(it) }
        }
    }

    override fun onBackPressed() {
        if (mBinding.wv.canGoBack()) {
            mBinding.wv.goBack()
            return
        }
        super.onBackPressed()
    }

    override fun onDestroy() {
        super.onDestroy()
        WebViewUtil.clearWebview(mBinding.wv)
    }

    companion object {
        fun goLaunch(mContext: Context, title: String, url: String) {
            val intent = Intent(mContext, CommonWebViewActivity::class.java)
            intent.putExtra("title", title)
            intent.putExtra("url", url)
            mContext.startActivity(intent)
        }
    }
}